package com.yunti.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yunti.business.domain.BaseUnit;
import com.yunti.business.service.IBaseUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单位Controller
 *
 * @author yangtt
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/business/unit")
public class BaseUnitController extends BaseController
{
    @Autowired
    private IBaseUnitService baseUnitService;

    /**
     * 查询单位列表
     */
    @PreAuthorize("@ss.hasPermi('business:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseUnit baseUnit)
    {
        startPage();
        List<BaseUnit> list = baseUnitService.selectBaseUnitList(baseUnit);
        return getDataTable(list);
    }

    @PostMapping("/getUnitList")
    public AjaxResult getUnitList(@RequestBody BaseUnit baseUnit)
    {
        List<BaseUnit> list = baseUnitService.selectBaseUnitList(baseUnit);
        return AjaxResult.success(list);
    }

    /**
     * 导出单位列表
     */
    @PreAuthorize("@ss.hasPermi('business:unit:export')")
    @Log(title = "单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseUnit baseUnit)
    {
        List<BaseUnit> list = baseUnitService.selectBaseUnitList(baseUnit);
        ExcelUtil<BaseUnit> util = new ExcelUtil<BaseUnit>(BaseUnit.class);
        util.exportExcel(response, list, "单位数据");
    }

    /**
     * 获取单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseUnitService.selectBaseUnitById(id));
    }

    /**
     * 新增单位
     */
    @PreAuthorize("@ss.hasPermi('business:unit:add')")
    @Log(title = "单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseUnit baseUnit)
    {
        return toAjax(baseUnitService.insertBaseUnit(baseUnit));
    }

    /**
     * 修改单位
     */
    @PreAuthorize("@ss.hasPermi('business:unit:edit')")
    @Log(title = "单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseUnit baseUnit)
    {
        return toAjax(baseUnitService.updateBaseUnit(baseUnit));
    }

    /**
     * 删除单位
     */
    @PreAuthorize("@ss.hasPermi('business:unit:remove')")
    @Log(title = "单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseUnitService.deleteBaseUnitByIds(ids));
    }
}
